import { RouteObject } from "react-router-dom";

/**
 * 根据：pathName 查找路由配置信息
 * 
 * @params routes 路由信息配置对象
 * @params pathName 页面路径
*/
const findRouteInfo = (routes: RouteObject[], pathName: string): RouteObject | undefined => {
  let routeInfo: RouteObject | undefined = undefined

  const walk = (routes: RouteObject[], pathName: string) => {
    routes.some((route) => {
      if (route.path === pathName) {
        routeInfo = route
        return true
      } else if (route.children && route.children.length > 0) {
        return walk(route.children, pathName) === undefined ? false : true
      } else {
        return false
      }
    })
  }
  walk(routes, pathName)
  
  return routeInfo
}

export default {
  findRouteInfo
}